export default class extends think.BaseController {
  __before() { // 前置操作，在action执行，返回false则终止当前控制器
  }

  async getPostListAction() { // 获取所有岗位列表
    const aj = think.ajaxJson();
    const data = this.get('page,pageSize,post_name');
    const PostService = think.service('sys/PostService');
    const res = await PostService.getPostList(data);
    if (res && res.error) {
      return this.errorJson('岗位列表查询失败');
    } else {
      return this.successJson(res);
    }
  }

  async savePostInfoAction() { // 保存岗位信息
    let saveData = {};
    const userData = this.getUser();
    if (this.post('id')) {
      saveData = this.post('id,post_name,post_type,post_sort,remarks');
    } else {
      saveData = this.post('post_name,post_type,post_code,post_sort,remarks');
      saveData.create_by = userData.userId;
      saveData.create_date = new Date();
    }
    saveData.update_by = userData.userId;
    saveData.update_date = new Date();
    const PostService = think.service('sys/PostService');
    const res = await PostService.savePostInfo(saveData);
    if (res) {
      if (res.error) {
        return this.errorJson('岗位信息保存失败');
      } else{
        return this.successJson();
      }
    } else{
      return this.errorJson('岗位名称或编码重复，请重新输入');
    }
  }

  async delPostInfoAction() { // 删除岗位
    const delData = {
      id: this.post('id')
    };
    const PostService = think.service('sys/PostService');
    const res = await PostService.delPostInfo(delData);
    if (res && res.error) {
      return this.errorJson('岗位信息删除失败');
    }else {
      return this.successJson();
    }
  }

  async getAllPostAction() {
    const PostService = think.service('sys/PostService');
    const res = await PostService.getAllPost();
    if (res && res.error) {
      return this.errorJson('岗位列表获取失败');
    }else {
      return this.successJson(res);
    }
  }

  async saveUserPostAction() {
    const PostService = think.service('sys/PostService');
    const data = {
      user_id: this.post('user_id'),
      delList: this.post('delList'),
      addList: this.post('addList')
    };
    const res = await PostService.saveUserPost(data);
    if (res && res.error) {
      return this.errorJson('用户岗位关联失败');
    } else{
      return this.successJson(res);
    }
  }
  __after() { // 后置操作,在action调用之后执行

  }
  __call() { // 魔术函数

  }
};
